package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.CardListBean;
import cn.wikiflyer.beikang.act.user.bean.CardListBean.CardBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 银行卡列表
 * 
 * @author qunlee_mr
 * 
 */
public class UserBalanceCardListAct extends BaseActivity implements
		WKListViewInterface {

	private PullToRefreshListView pullListView;
	private WKListViewUtils<CardBean, CardListBean> listUtils;

	// 是否只是为了选择
	private boolean select = false;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_tixian);
	}

	@Override
	public void init() {
		getHeader().setTitle("提现银行卡");

		getHeader().setRightTxt("新增");
		getHeader().setClickListener(null, null, new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(ctx, UserBalanceCardAddAct.class));
			}
		});

		listUtils = new WKListViewUtils<CardBean, CardListBean>() {
		};
		pullListView = (PullToRefreshListView) super
				.findViewById(R.id.wk_pull_refresh_list);
		listUtils.init(ctx, this, pullListView);

		select = getIntent().getExtras().getBoolean("select");
		if (select) {
			getHeader().setTitle("请选择银行卡");
		}
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		listUtils.updateData();
		System.out.println("onRestart");
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getBankCards(isLocal);

	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<CardListBean>>() {
		}.getType();
		Gson gson = new Gson();
		BKBaseBean<CardListBean> base = gson.fromJson(result, cvbType);
		listUtils.list.clear();
		listUtils.list.addAll(base.data.list);
		listUtils.adapter.notifyDataSetChanged();

		// 更新cardBean
		if (UserBalanceCashAct.cardBean == null) {
			return;
		}
		String cardId = UserBalanceCashAct.cardBean.id;
		boolean needClear = true;
		for (int i = 0; i < base.data.list.size(); i++) {
			if (base.data.list.get(i).id.equals(cardId)) {
				needClear = false;
				break;
			}
		}
		if (needClear) {
			UserBalanceCashAct.cardBean = null;
		}

		// CardListBean cardList = new CardListBean();
		// for (int i = 0; i < 5; i++) {
		// CardBean card = cardList.new CardBean();
		// card.bankName = "招商银行";
		// card.cardNum = "2514****231";
		// card.username = "PeeKid";
		// card.if_def = i;
		// listUtils.list.add(card);
		// }
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_user_balance_card_item;
	}

	class MyViewHolder extends WKViewHolder<CardBean> {

		TextView nameView;
		TextView bankView;
		// TextView defView;
		TextView numView;
		View click_view;

		public MyViewHolder(View view) {
			nameView = (TextView) view.findViewById(R.id.card_name);
			bankView = (TextView) view.findViewById(R.id.card_bank);
			// defView = (TextView) view.findViewById(R.id.card_def);
			numView = (TextView) view.findViewById(R.id.card_num);
			click_view = view.findViewById(R.id.click_view);
		}

		@Override
		public void setData(CardBean t) {
			if (t.is_default.equals("1")) {
				bankView.setText("开户：	" + t.bankName + "       -  默认提现银行");
			} else {
				bankView.setText("开户：	" + t.bankName);
			}
			nameView.setText("姓名：	" + t.cardName);
			numView.setText("卡号：	" + t.cardNumber);

			click_view.setTag(t);
			click_view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					CardBean t = (CardBean) v.getTag();
					if (select) {// 选择
						UserBalanceCashAct.cardBean = t;
						finish();
					} else {// 浏览
						Intent i = new Intent(ctx, UserBalanceCardAddAct.class);
						i.putExtra("card", t);
						startActivity(i);
					}
				}
			});

		}
	}

}
